package com.soul.soulcommon.detail.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.soul.soulcommon.detail.GuardianDetail;
import com.soul.soulcommon.dto.Jwt;
import com.soul.souldao.domain.Guardian;
import com.soul.souldao.mapper.GuardianMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

@Slf4j
@Service
public class GuardianDetailService implements UserDetailsService {
    @Autowired
    private GuardianMapper guardianMapper;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        log.info("监护人进行登录===========================");
        Guardian guardian = guardianMapper.selectOne(new LambdaQueryWrapper<Guardian>().eq(Guardian::getUserName, userName));
        if(guardian == null){
            throw new UsernameNotFoundException("用户或密码不正确");
        }
        Jwt jwt = new Jwt(guardian.getId(), guardian.getUserName(), guardian.getPassword(), guardian.getRoleName());
        GuardianDetail guardianDetail = new GuardianDetail(jwt);
        return guardianDetail;
    }
}
